/*
 * Copyright 2012 Benoît Combe <benoit.combe@gmail.com>.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package fr.ipst.spot.formulaires.entites;

import fr.ipst.spot.entites.AbstractSpotEntite;
import fr.ipst.spot.exceptions.SpotExceptionForm;
import fr.ipst.spot.formulaires.FormAbstractHtml;
import javax.servlet.http.HttpServletRequest;

/**
 *
 * @param <T>
 * @author Benoît Combe <benoit.combe@gmail.com>
 */
public abstract class FormAbstractHtmlEntiteId<T extends AbstractSpotEntite> extends FormAbstractHtml {

    /**
     *
     */
    public final static Integer defaultId = -1;
    private Integer id;

    /**
     *
     * @param id
     */
    public FormAbstractHtmlEntiteId(Integer id) {
        this.id = id;
    }

    /**
     *
     * @param request
     * @param entityClass
     */
    public FormAbstractHtmlEntiteId(HttpServletRequest request, Class<T> entityClass) {
        this.id = this.getIntegerFromRequest(request, "id" + entityClass.getSimpleName(), defaultId);
    }

    /**
     *
     * @throws SpotExceptionForm
     */
    public void validate() throws SpotExceptionForm {
        try {
            this.isId(id);
        } catch (SpotExceptionForm e) {
            throw new SpotExceptionForm("Id fournit invalide");
        }
    }

    /**
     *
     * @return
     */
    public Integer getId() {
        return id;
    }

    /**
     *
     * @param id
     */
    public void setId(Integer id) {
        this.id = id;
    }

    /**
     *
     * @return
     */
    public boolean isValid() {
        return this.id != defaultId;
    }
}
